import api from '@/api'

// 财务状态模块
const state = {
  reimbursementList: [],
  currentReimbursement: null,
  invoiceList: [], // 发票列表
  pagination: {
    total: 0,
    currentPage: 1,
    pageSize: 10
  },
  loading: false,
  stats: {
    pending: 0,
    approved: 0,
    rejected: 0,
    total: 0
  }
}

const mutations = {
  SET_LOADING(state, status) {
    state.loading = status
  },
  SET_REIMBURSEMENT_LIST(state, data) {
    state.reimbursementList = data.list || []
    if (data.pagination) {
      state.pagination = {
        ...state.pagination,
        ...data.pagination
      }
    }
  },
  SET_CURRENT_REIMBURSEMENT(state, reimbursement) {
    state.currentReimbursement = reimbursement
  },
  SET_STATS(state, stats) {
    state.stats = stats
  },
  // 设置发票列表
  SET_INVOICE_LIST(state, invoices) {
    state.invoiceList = invoices
  },
  // 更新发票状态
  UPDATE_INVOICE_STATUS(state, { invoiceId, status }) {
    const index = state.invoiceList.findIndex(invoice => invoice.id === invoiceId)
    if (index !== -1) {
      state.invoiceList[index].status = status
    }
  }
}

const actions = {
  // 获取报销申请列表
  getReimbursementList({ commit }, params = {}) {
    commit('SET_LOADING', true)
    return api.finance.getReimbursementList(params)
      .then(res => {
        commit('SET_REIMBURSEMENT_LIST', res.data)
        return res.data
      })
      .finally(() => {
        commit('SET_LOADING', false)
      })
  },
  
  // 获取报销申请详情
  getReimbursementDetail({ commit }, id) {
    commit('SET_LOADING', true)
    return api.finance.getReimbursementDetail(id)
      .then(res => {
        commit('SET_CURRENT_REIMBURSEMENT', res.data)
        return res.data
      })
      .finally(() => {
        commit('SET_LOADING', false)
      })
  },
  
  // 提交报销申请
  submitReimbursement({ commit, dispatch }, data) {
    commit('SET_LOADING', true)
    
    // 处理发票状态更新
    const invoicesToUpdate = []
    if (data.items) {
      data.items.forEach(item => {
        if (item.invoiceId) {
          invoicesToUpdate.push(item.invoiceId)
        }
      })
    }
    
    return api.finance.submitReimbursement(data)
      .then(res => {
        // 更新已关联发票的状态为已使用
        invoicesToUpdate.forEach(invoiceId => {
          dispatch('updateInvoiceStatus', { invoiceId, status: 'used' })
        })
        return res.data
      })
      .finally(() => {
        commit('SET_LOADING', false)
      })
  },
  
  // 获取财务统计
  getFinanceStats({ commit }) {
    return api.finance.getFinanceStats()
      .then(res => {
        commit('SET_STATS', res.data)
        return res.data
      })
  },
  
  // 获取可用发票列表
  getInvoiceList({ commit }, params = {}) {
    commit('SET_LOADING', true)
    return api.finance.getInvoiceList(params)
      .then(res => {
        commit('SET_INVOICE_LIST', res.data.list || [])
        return res.data
      })
      .finally(() => {
        commit('SET_LOADING', false)
      })
  },
  
  // 更新发票状态
  updateInvoiceStatus({ commit }, { invoiceId, status }) {
    return api.finance.updateInvoiceStatus(invoiceId, status)
      .then(res => {
        commit('UPDATE_INVOICE_STATUS', { invoiceId, status })
        return res.data
      })
  }
}

const getters = {
  // 待审批的报销申请数量
  pendingCount: state => state.stats.pending || 0,
  
  // 可用于报销的发票列表（已验证状态）
  availableInvoices: state => state.invoiceList.filter(invoice => invoice.status === 'verified'),
  
  // 已使用的发票列表
  usedInvoices: state => state.invoiceList.filter(invoice => invoice.status === 'used')
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
} 